{% extends "base.html" %}

{% block title %}项目管理{% endblock %}

{% block content %}
<div class="container">
    <div class="row mb-4">
        <div class="col">
            <h2 class="mb-3">项目管理</h2>
            {% if current_user.role == 'engineer' or current_user.role == 'manager' %}
            <a href="{{ url_for('main.create_project') }}" class="btn btn-primary">
                <i class="fas fa-plus-circle me-1"></i>新建项目
            </a>
            {% endif %}
        </div>
    </div>

    <!-- 搜索表单 -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-4">
                    <div class="input-group">
                        <span class="input-group-text"><i class="fas fa-search"></i></span>
                        <input type="text" class="form-control" name="search" value="{{ search }}" 
                               placeholder="搜索项目名称或编号">
                    </div>
                </div>
                <div class="col-md-3">
                    <select class="form-select" name="project_type">
                        <option value="">所有类型</option>
                        <option value="industrial" {% if project_type == 'industrial' %}selected{% endif %}>工业测控</option>
                        <option value="environmental" {% if project_type == 'environmental' %}selected{% endif %}>环境监测</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="fas fa-search me-1"></i>搜索
                    </button>
                </div>
                <div class="col-md-2">
                    <a href="{{ url_for('main.projects') }}" class="btn btn-secondary w-100">
                        <i class="fas fa-redo me-1"></i>重置
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- 项目列表 -->
    <div class="row">
        {% for project in pagination.items %}
        <div class="col-md-6 col-lg-4 mb-4">
            <div class="card h-100">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">{{ project.name }}</h5>
                    <span class="badge {% if project.status == 'draft' %}bg-secondary
                                      {% elif project.status == 'pending' %}bg-warning
                                      {% elif project.status == 'approved' %}bg-success
                                      {% elif project.status == 'rejected' %}bg-danger
                                      {% else %}bg-secondary{% endif %}">
                        {% if project.status == 'draft' %}草稿
                        {% elif project.status == 'pending' %}待审核
                        {% elif project.status == 'approved' %}已批准
                        {% elif project.status == 'rejected' %}已拒绝
                        {% else %}未知状态{% endif %}
                    </span>
                </div>
                <div class="card-body">
                    <p><strong>项目编号：</strong>{{ project.project_code }}</p>
                    <p><strong>项目类型：</strong>
                        {% if project.project_type == 'industrial' %}
                            工业测控
                        {% elif project.project_type == 'environmental' %}
                            环境监测
                        {% else %}
                            未知类型
                        {% endif %}
                    </p>
                    <p><strong>创建时间：</strong>{{ project.created_at.strftime('%Y-%m-%d') }}</p>
                </div>
                <div class="card-footer bg-transparent">
                    <div class="btn-group w-100">
                        <a href="{{ url_for('main.project_detail', project_id=project.id) }}" 
                           class="btn btn-outline-primary">
                            <i class="fas fa-info-circle me-1"></i>详情
                        </a>
                        {% if current_user.role == 'engineer' and project.creator_id == current_user.id %}
                        <a href="{{ url_for('main.edit_project', project_id=project.id) }}" 
                           class="btn btn-outline-secondary">
                            <i class="fas fa-edit me-1"></i>编辑
                        </a>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
        {% else %}
        <div class="col-12">
            <div class="alert alert-info">
                <i class="fas fa-info-circle me-2"></i>暂无项目数据
            </div>
        </div>
        {% endfor %}
    </div>

    <!-- 分页控件 -->
    {% if pagination.pages > 1 %}
    <nav aria-label="Page navigation" class="mt-4">
        <ul class="pagination justify-content-center">
            <li class="page-item {% if not pagination.has_prev %}disabled{% endif %}">
                <a class="page-link" href="{% if pagination.has_prev %}{{ url_for('main.projects', page=pagination.prev_num, search=search, project_type=project_type) }}{% else %}#{% endif %}">
                    <i class="fas fa-chevron-left"></i>
                </a>
            </li>
            {% for page in pagination.iter_pages(left_edge=2, left_current=2, right_current=3, right_edge=2) %}
                {% if page %}
                    <li class="page-item {% if page == pagination.page %}active{% endif %}">
                        <a class="page-link" href="{{ url_for('main.projects', page=page, search=search, project_type=project_type) }}">{{ page }}</a>
                    </li>
                {% else %}
                    <li class="page-item disabled"><span class="page-link">...</span></li>
                {% endif %}
            {% endfor %}
            <li class="page-item {% if not pagination.has_next %}disabled{% endif %}">
                <a class="page-link" href="{% if pagination.has_next %}{{ url_for('main.projects', page=pagination.next_num, search=search, project_type=project_type) }}{% else %}#{% endif %}">
                    <i class="fas fa-chevron-right"></i>
                </a>
            </li>
        </ul>
    </nav>
    {% endif %}
</div>
{% endblock %} 